<?php
/**
 *
 * @author Nikita
 *
 */
class Lemar_Thumbnail
{
	
	const RESIZE_WIDTH = 100;
	const RESIZE_WIDTH_TMB = 120;
	const RESIZE_HEIGHT = 100;

	const MAIN_WIDTH = 200;
	
	const BANNER_RESIZE_WIDTH = 950;
	const BANNER_RESIZE_HEIGHT = 220;	
    /**
    * create image from file with new width and height
    *
    * @param string $source file name
    * @param string $target new file name
    * @param string $type file extention
    * @param int $new_width new width
    * @param int $new_height new height
    * @return nothing
    */
    public static function createThumbnailCropped($source, $target, $type='', $new_width='', $new_height='')
    {
        ini_set("memory_limit", "500M");
        ini_set("max_execution_time", "0");

        $sizes = @getimagesize($source);

        $ow = $sizes[0];
        $oh = $sizes[1];

        if($type == '')
        {
            $aExt = explode('.', $source);
            $type = end($aExt);
        }

        if($new_width == '' || $new_height == '' )
        {
//            $sImg = 'image';
//            $oConfig = Zend_Registry::getInstance()->get('config')->$sImg;
//            $new_width = $oConfig->logoWidth;
//            $new_height = $oConfig->logoHeight;
            $new_width 	= self::RESIZE_WIDTH;
            $new_height = self::RESIZE_HEIGHT;
        }

	    if (($oh <= $new_height) && ($ow <= $new_width))
	    {
	        copy($source, $target);
	        return;
	    }
        $img = null;

	    //determine function by image type
	    $type = Lemar_Image::filenameToMime($source);
	    if($type == 'jpg' || $type == 'jpeg')
	    {
	       $img = imagecreatefromjpeg($source);
	    }
	    elseif($type == 'gif')
	    {
	    	$img = imagecreatefromgif($source);
	    }
	    elseif($type == 'png')
	    {
	    	$img = imagecreatefrompng($source);
	    }
	    if ($img)
	    {
	        $target_id = imageCreateTrueColor($new_width, $new_height);

	        $width_ratio  = $ow/$new_width;
	        $height_ratio = $oh/$new_height;

	        if ($width_ratio > $height_ratio)
	        {
	            $x_offset = round($ow/2 - ($new_width/2) * $height_ratio);
	            ImageCopyResampled(
	                    $target_id, $img,
	                    0,0,
	                    $x_offset,0,
	                    $new_width, $new_height,
	                    ($new_width * $height_ratio), $oh
	                );
	        }
	        else
	        {
	            ImageCopyResampled(
	                $target_id, $img,
	                0,0, 0,0,
	                $new_width, $new_height,
	                $ow, ($new_height * $width_ratio)
	            );
	        }
	        if($type == 'jpg' || $type == 'jpeg')
	        {
		        imagejpeg($target_id, $target, 75);
	        }
	        elseif($type == 'gif')
	        {
		        imagegif($target_id, $target);
	        }
	        elseif($type == 'png')
	        {
		        imagepng($target_id, $target);
	        }
	        chmod($target,octdec('0775'));
        }
    }

    /**
     * Get path to the logo image
     *
     * @param integer $iWeddingId wedding id
     * @return string image path
     */
    public static function getImageLogoPath($iWeddingId)
    {
        $sImg = 'image';
        $oConfig = Zend_Registry::getInstance()->get('config')->$sImg;

    	$sImagePath1 = ($oConfig->uploadPath).$iWeddingId;

    	if( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpeg') )
    	{
    		$sImagePath = $sImagePath1.'.jpeg';
    	}
        elseif( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpg') )
        {
            $sImagePath = $sImagePath1.'.jpg';
        }
        elseif( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.gif') )
        {
            $sImagePath = $sImagePath1.'.gif';
        }
        elseif( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.png') )
        {
            $sImagePath = $sImagePath1.'.png';
        }
    	else
    	{
            $sImagePath = '';
    	}

    	return $sImagePath;
    }

    /**
     * delete image logo file
     *
     * @param integer $iWeddingId wedding id
     * @return nothing
     */
    public static function deleteImage($iWeddingId)
    {
    	$sImg = 'image';
        $oConfig = Zend_Registry::getInstance()->get('config')->$sImg;

    	$sImagePath1 = ($oConfig->uploadPath).$iWeddingId;

        if( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpeg') )
        {
            unlink($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpeg');
        }
        if( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpg') )
        {
            unlink($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.jpg');
        }
        if( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.gif') )
        {
            unlink($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.gif');
        }
        if( file_exists($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.png') )
        {
            unlink($_SERVER['DOCUMENT_ROOT'].$sImagePath1.'.png');
        }
    }

    /**
     * check image file extantion
     *
     * @param $type file extantion
     * @return bool
     */
    public static function checkImageType($type)
    {
        if($type == 'jpeg' || $type == 'jpg' || $type == 'gif' || $type == 'png' )
        {
            return true;
        }
        return false;
    }
}
